package com.yfyszk.kingbird.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yfyszk.kingbird.document.Book;
import com.yfyszk.kingbird.document.EsEntity;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EsService
 * @Description: TODO
 * @Author wgc
 * @Date 2020/12/17 12:36
 * @Version V1.0
 **/
@Service
public class EsService {
    @Autowired
    private EsUtil esUtil;

    public List<Book> getAll() {
        return esUtil.search(EsUtil.INDEX_NAME, new SearchSourceBuilder(), Book.class);
    }

    public Book getByBookId(int bookId) {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(new TermQueryBuilder("bookId", bookId));
        List<Book> res = esUtil.search(EsUtil.INDEX_NAME, builder, Book.class);
        if (res.size() > 0) {
            return res.get(0);
        } else {
            return null;
        }
    }

    public List<Book> searchByKey(String key) {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", key));
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.size(10).query(boolQueryBuilder);
        return esUtil.search(EsUtil.INDEX_NAME, builder, Book.class);
    }

    public IndexResponse putOne(Book book) {
        EsEntity<Book> entity = new EsEntity<>(book.getBookId() + "", book);
        return esUtil.insertOrUpdateOne(EsUtil.INDEX_NAME, entity);
    }

    public BulkResponse putBatch(List<Book> books) {
        List<EsEntity> list = new ArrayList<>();
        books.forEach(item -> list.add(new EsEntity<>(item.getBookId() + "", item)));
        return esUtil.insertBatch(EsUtil.INDEX_NAME, list);
    }

    public BulkByScrollResponse deleteById(int id) {
        return esUtil.deleteByQuery(EsUtil.INDEX_NAME, new TermQueryBuilder("bookId", id));
    }

    public BulkResponse deleteBatch(List<Integer> list) {
        return esUtil.deleteBatch(EsUtil.INDEX_NAME, list);
    }

    public PageInfo searchByPage(SearchSourceBuilder searchSourceBuilder,int pageNum,int pageSize,Book book) throws Exception {
        PageInfo<Book> bookPageInfo = esUtil.searchByPage(searchSourceBuilder, pageNum, pageSize, Book.class);
        return  bookPageInfo;
    }
}
